<template>
  <div class="log-box">
    <div class="log-form">
      <h2 class="title">用户登录</h2>
      <el-form :model="userInfo" :rules="rules" :label-width="80" center>
        <el-form-item label="用户名" prop="username">
          <el-input
            v-model="userInfo.username"
            placeholder="请输入用户名"
            prefix-icon="User"
          ></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input
            prefix-icon="Lock"
            v-model="userInfo.password"
            placeholder="请输入密码"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-button type="primary" class="w-80px" @click="submit">登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { reactive } from "vue";
import { useUserStore } from "@/stores/user";
import { useRoute, useRouter } from "vue-router";

const router = useRouter();
const route = useRoute();

const { login } = useUserStore();

const userInfo = reactive({
  username: "",
  password: "",
});

const rules = reactive({
  username: [
    {
      required: true,
      message: "请输入用户名",
      trigger: "blur",
    },
  ],
  password: [
    {
      required: true,
      message: "请输入密码",
      trigger: "blur",
    },
  ],
});

async function submit() {
  login(userInfo).then(() => {
    const redirect = route.query.redirect as string;
    if (redirect) {
      router.push(redirect + "");
    } else {
      router.push("/index");
    }
  });
}
</script>

<style lang="less" scoped>
.log-box {
  width: 100%;
  height: 100%;
  background-color: white;
  position: relative;

  .title {
    text-align: center;
    margin-bottom: 20px;
  }

  .log-form {
    width: 400px;
    min-height: 600px;
    background-color: rgba(143, 147, 150, 0.116);
    position: absolute;
    right: 200px;
    top: 50%;
    border-radius: 10px;
    padding: 20px;
    transform: translateY(-50%);
  }
}
</style>
